package com.jzy.gulimall.seckill.interceptor;

import com.jzy.common.constant.AuthServerConstant;
import com.jzy.common.utils.SsoClientUtil;
import com.jzy.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author jzy
 * @date 2020/9/20 15:41
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    //存储用户信息
    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute != null) {
            loginUser.set(attribute);
            return true;
        } else {
            //重定向去登录
            SsoClientUtil.redirectToSso(request, response);
            return false;
        }
    }
}
